<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NapCat Webhook - 首次运行配置</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        
        .container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            padding: 40px;
            max-width: 500px;
            width: 100%;
        }
        
        .header {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .logo {
            font-size: 3em;
            margin-bottom: 10px;
        }
        
        h1 {
            color: #333;
            margin-bottom: 10px;
            font-size: 24px;
        }
        
        .subtitle {
            color: #666;
            font-size: 14px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 5px;
            color: #333;
            font-weight: 500;
        }
        
        input, select {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 14px;
            transition: all 0.3s ease;
        }
        
        input:focus, select:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        
        .form-section {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 10px;
            margin-bottom: 25px;
        }
        
        .form-section h3 {
            color: #495057;
            margin-bottom: 15px;
            font-size: 16px;
            display: flex;
            align-items: center;
        }
        
        .form-section h3::before {
            content: '📌';
            margin-right: 8px;
        }
        
        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 15px 30px;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            width: 100%;
            transition: all 0.3s ease;
        }
        
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }
        
        .help-text {
            font-size: 12px;
            color: #6c757d;
            margin-top: 5px;
        }
        
        .success-message {
            background: #d4edda;
            color: #155724;
            padding: 15px;
            border-radius: 8px;
            text-align: center;
            margin-bottom: 20px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <div class="logo">🤖</div>
            <h1>NapCat Webhook 配置</h1>
            <p class="subtitle">首次运行需要进行基本配置</p>
        </div>
        
        <div class="success-message" id="successMessage">
            ✅ 配置完成！正在启动服务...
        </div>
        
        <form id="setupForm">
            <div class="form-section">
                <h3>NapCat 服务器配置</h3>
                
                <div class="form-group">
                    <label for="napcatUrl">服务器地址</label>
                    <input 
                        type="url" 
                        id="napcatUrl" 
                        value="http://192.168.5.4:3000"
                        placeholder="http://192.168.5.4:3000"
                        required>
                    <div class="help-text">NapCat 服务的 HTTP 地址</div>
                </div>
                
                <div class="form-group">
                    <label for="token">访问令牌 (Token)</label>
                    <input 
                        type="text" 
                        id="token" 
                        placeholder="留空如果无需验证"
                        >
                    <div class="help-text">在 NapCat 配置中设置的访问令牌</div>
                </div>
            </div>
            
            <div class="form-section">
                <h3>Webhook 配置</h3>
                
                <div class="form-group">
                    <label for="port">服务端口</label>
                    <input 
                        type="number" 
                        id="port" 
                        value="8080"
                        min="1000" 
                        max="65535"
                        required>
                    <div class="help-text">Webhook 接收服务监听的端口</div>
                </div>
                
                <div class="form-group">
                    <label for="webhookSecret">Webhook 密钥</label>
                    <input 
                        type="text" 
                        id="webhookSecret" 
                        placeholder="留空如果无需签名验证"
                        >
                    <div class="help-text">用于验证 Webhook 请求的签名密钥</div>
                </div>
            </div>
            
            <button type="submit" class="btn">保存配置并启动</button>
        </form>
    </div>

    <script>
        document.getElementById('setupForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const formData = {
                napcatUrl: document.getElementById('napcatUrl').value,
                token: document.getElementById('token').value,
                port: document.getElementById('port').value,
                webhookSecret: document.getElementById('webhookSecret').value
            };
            
            const btn = this.querySelector('.btn');
            const originalText = btn.textContent;
            btn.textContent = '配置中...';
            btn.disabled = true;
            
            try {
                const response = await fetch('/api/setup', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(formData)
                });
                
                const result = await response.json();
                
                if (result.success) {
                    document.getElementById('successMessage').style.display = 'block';
                    setTimeout(() => {
                        window.location.href = '/';
                    }, 2000);
                } else {
                    alert('配置失败: ' + (result.message || '未知错误'));
                    btn.textContent = originalText;
                    btn.disabled = false;
                }
            } catch (error) {
                alert('配置请求失败: ' + error.message);
                btn.textContent = originalText;
                btn.disabled = false;
            }
        });
    </script>
</body>
</html>